package com.tp2.etna.costos.constante;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Util {
	
	
	public static Date primerDia(Date d) throws ParseException{
		Calendar cal = Calendar.getInstance();
		cal.setTime(d);
		Integer mes	=(cal.get(cal.MONTH)+1);
		String mes2 =mes.toString();
		
		if(mes<10){
			mes2 ="0" + mes;
		}
		Integer anio	=cal.get(cal.YEAR);
		String anio2=anio.toString();
		
		DateFormat formatter;
		
		formatter = new SimpleDateFormat("dd/MM/yyyy");
		Date date = (Date) formatter.parse("01/" + mes2 + "/" +anio2);
		
		return date;
	}
	
	public static Date getUltimoDiaDelMes(Date date) {
			        Calendar cal = Calendar.getInstance();
			        cal.setTime(date);
			        cal.set(cal.get(Calendar.YEAR),
			                cal.get(Calendar.MONTH),
			                cal.getActualMaximum(Calendar.DAY_OF_MONTH),
			                cal.getMaximum(Calendar.HOUR_OF_DAY),
			                cal.getMaximum(Calendar.MINUTE),
			                cal.getMaximum(Calendar.SECOND));
			        return cal.getTime();
	}
	
	public static Date getPeriodo(Integer mes, Integer anio){
		int year = anio;
	    int month = mes;
	    int day = 1;

	    String date = year + "/" + month + "/" + day;
	    Date utilDate = null;

	    try {
	      SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
	      utilDate = formatter.parse(date);
	      System.out.println("utilDate:" + utilDate);
	    } catch (ParseException e) {
	      System.out.println(e.toString());
	      e.printStackTrace();
	    }
	    return utilDate;
	}

}
